package com.hefu.virtuallife.customview.ablgrid;

import android.content.Context;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.hefu.virtuallife.R;

/**
 * Created by pc on 2018/7/27.
 */

public class AblGridViewBaseAdapter extends BaseAdapter {

    private Context mContext;
    private String[] scenes;
    private int[] sceneImgs;//显示图标数组
    private int[] smartImgs;//显示图标数组

    public AblGridViewBaseAdapter(Context mContext, String[] scenes, int[] sceneImgs, int[] smartImgs) {
        super();
        this.mContext = mContext;
        this.sceneImgs = sceneImgs;
        this.smartImgs = smartImgs;
        this.scenes = scenes;
    }

    @Override
    public int getCount() {
        return sceneImgs.length;
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        convertView = loadData(position, convertView, parent);
        return convertView;
    }

    /**
     * 加载会员或医生的功能列表
     */
    public View loadData(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(
                    R.layout.home_gridview_item, parent, false);
        }
        ImageView iv_scene = get(convertView, R.id.id_scene);
        ImageView iv_smart = get(convertView, R.id.id_smart_list);
        TextView tv_scene_name = get(convertView, R.id.tv_scene_name);
        tv_scene_name.setText(scenes[position]);
        iv_scene.setBackgroundResource(sceneImgs[position]);
        iv_smart.setBackgroundResource(smartImgs[position]);
        return convertView;
    }

    public <T extends View> T get(View view, int id) {
        SparseArray<View> viewHolder = (SparseArray<View>) view.getTag();
        if (viewHolder == null) {
            viewHolder = new SparseArray<View>();
            view.setTag(viewHolder);
        }
        View childView = viewHolder.get(id);
        if (childView == null) {
            childView = view.findViewById(id);
            viewHolder.put(id, childView);
        }
        return (T) childView;
    }

}